# ๐งก 02. ๋๋ค(lambda) | ํจ์ํ ์ธํฐํ์ด์ค(Predicate, Consumer, Function)
# ๐ ๋๋ค (lambda)
๋๋ค ํํ์
์ ๋ฉ์๋๋ก ์ ๋ฌํ ์ ์๋ ์ต๋ช
ํจ์๋ฅผ ๋จ์ํ ํ ๊ฒ.
๋๋ค์ ํน์ง์ ์๋์ ๊ฐ๋ค.
TIP
- ์ต๋ช
- ๋ณดํต์ ๋ฉ์๋์ ๋ฌ๋ฆฌ ์ด๋ฆ์ด ์์ด์ ์ต๋ช !
- ํจ์
- ํน์ ํด๋์ค์ ์ข ์๋์ง ์์์ ํจ์๋ผ๊ณ ๋ถ๋ฅธ๋ค.
- ์ ๋ฌ
- ๋๋ค ํํ์์ ๋ฉ์๋ ์ธ์๋ก ์ ๋ฌํ๊ฑฐ๋ ๋ณ์๋ก ์ ์ฅํ ์ ์๋ค.
- ๊ฐ๊ฒฐ์ฑ
- ์ต๋ช ํด๋์ค์ฒ๋ผ ๋ง์ ์์ง๊ตฌ๋ ํ ์ฝ๋๋ฅผ ๊ตฌํํ ํ์๊ฐ ์๋ค.
# ๐ ์ฌ๊ณผ ํด๋์ค
๋ฌด๊ฒ์ ์์ ์ ๋ณด๊ฐ ๋ด๊ธด ์ฌ๊ณผ ํด๋์ค ์์ฑ
package javaStudy.mda02;
/**
* ์ฌ๊ณผ
*/
public class Apple {
public enum Color {
RED,
GREEN
}
Color color;
Integer weight;
public Apple(Color color, int weight) {
this.color = color;
this.weight = weight;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
public Integer getWeight() {
return weight;
}
public void setWeight(Integer weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Apple{" +
"color=" + color +
", weight=" + weight +
'}';
}
}
# ๊ธฐ์กด ์ฝ๋
Comparator<Apple> byWeight = new Comparator<Apple>() {
@Override
public int compare(Apple o1, Apple o2) {
return o1.getWeight().compareTo(o2.getWeight());
}
};
# ๋๋ค ์ ์ฉ ์ฝ๋
Comparator<Apple> byWeight = (Apple o1, Apple o2) -> o1.getWeight().compareTo(o2.getWeight());
# ๐ ํจ์ํ ์ธํฐํ์ด์ค
Predicate<T>
์ ๊ฐ์ ๊ฒ ํจ์ํ ์ธํฐํ์ด์ค๋ค. ๋ง์ ๋ํดํธ ๋ฉ์๋(์ธํฐํ์ด์ค์ ๋ฉ์๋๋ฅผ ๊ตฌํํ์ง ์์ ํด๋์ค๋ฅผ ๊ณ ๋ คํด์ ๊ธฐ๋ณธ ๊ตฌํ์ ์ ๊ณตํ๋ ๋ฐ๋๋ฅผ ํฌํจํ๋ ๋ฉ์๋)๊ฐ ์๋๋ผ๋ ์ถ์๋ฉ์๋๊ฐ ์ค์ง ํ๋๋ฉด ํจ์ํ ์ธํฐํ์ด์ค๋ค.
์๋ฅผ ๋ค์ด ์๋์ ๊ฐ์ ๊ฒ๋ค์ด ์๋ค.
# Predicate
test
๋ผ๋ ์ถ์ ๋ฉ์๋๋ฅผ ์ ์ํ๋ฉฐ boolean
์ ๋ฐํํ๋ค.
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
// ํ์ฉ ์์
package javaStudy.mda03;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class MainMethod {
public static void main(String[] args) {
List<String> listOfStrings = new ArrayList<>(Arrays.asList("apple","banana","melon", ""));
Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
// [apple, banana, melon]
List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
}
public static <T> List<T> filter(List<T> list, Predicate<T> p) {
List<T> results = new ArrayList<>();
for (T t : list) {
if (p.test(t)) {
results.add(t);
}
}
return results;
}
}
# Consumer
accept
๋ผ๋ ์ถ์๋ฉ์๋๋ฅผ ์ ์ํ๋ฉฐ void
๋ฅผ ๋ฐํํ๋ค.
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
// ํ์ฉ ์์
package javaStudy.mda03;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class ConsumerTest {
public static void main(String[] args) {
forEach(
Arrays.asList(1,2,3,4,5),
(Integer i) -> System.out.println(i)
);
}
public static <T> void forEach(List<T> list, Consumer<T> c) {
for(T t : list) {
c.accept(t);
}
}
}
# Function
apply
๋ผ๋ ์ถ์๋ฉ์๋๋ฅผ ์ ์ํ๋ฉฐ, ์ ๋ค๋ฆญ ํ์ T
๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋ฐ์ R
๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ค.
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
// ํ์ฉ
package javaStudy.mda03;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
public class FunctionTest {
public static void main(String[] args) {
List<Integer> l = map(
Arrays.asList("labmdas", "in", "action"),
(String s) -> s.length()
);
}
public static <T, R> List<R> map(List<T> list, Function<T, R> f) {
List<R> result = new ArrayList<>();
for(T t : list) {
result.add(f.apply(t));
}
return result;
}
}
# ๐ ๋ฉ์๋ ์ฐธ์กฐ
# ๊ธฐ์กด์ฝ๋
appleList.sort((Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
# ๋ฉ์๋ ์ฐธ์กฐ
appleList.sort(Comparator.comparing(Apple::getWeight));
appleList.sort(Comparator.comparing(Apple::getWeight).reversed()); // ๋ด๋ฆผ์ฐจ์
appleList.sort(Comparator.comparing(Apple::getWeight).reversed().thenComparing(Apple::getColor)); // ๋ฌด๊ฒ๊ฐ ๊ฐ์ผ๋ฉด ์์๋ณ๋ก ์ ๋ ฌ
# ๐ Predicate ์กฐํฉ
# apple List ์์ฑ ๋ฐ filter ๋ฉ์๋ ์์ฑ
public static void main(String[] args) {
List<Apple> appleList = Arrays.asList(
new Apple(Apple.Color.RED, 180),
new Apple(Apple.Color.GREEN, 100),
new Apple(Apple.Color.RED, 100),
new Apple(Apple.Color.GREEN, 350),
new Apple(Apple.Color.RED, 400)
);
}
public static <T> List<T> filter(List<T> list, java.util.function.Predicate<T> p) {
List<T> results = new ArrayList<>();
for (T t : list) {
if (p.test(t)) {
results.add(t);
}
}
return results;
}
# ๊ธฐ์กด Predicate
// [Apple{color=GREEN, weight=100}, Apple{color=GREEN, weight=350}]
Predicate<Apple> greenApplePredicate = (apple) -> Apple.Color.GREEN.equals(apple.getColor());
filter(appleList, greenApplePredicate);
# negate()
๊ธฐ์กด Predicate
๋ฐ์
// [Apple{color=RED, weight=180}, Apple{color=RED, weight=100}, Apple{color=RED, weight=400}]
Predicate<Apple> notGreenApple = greenApplePredicate.negate();
filter(appleList, notGreenApple);
# and
Predicate
์ฐ๊ฒฐ
// [Apple{color=GREEN, weight=350}]
Predicate<Apple> greenAndHeavyApple = greenApplePredicate.and(apple -> apple.getWeight() > 150);
filter(appleList, greenAndHeavyApple);
# or
Predicate
์ถ๊ฐ์กฐ๊ฑด
// [Apple{color=RED, weight=180}, Apple{color=RED, weight=100}, Apple{color=GREEN, weight=350}, Apple{color=RED, weight=400}]
Predicate<Apple> greenAndHeavyAppleOrRed = greenApplePredicate.and(apple -> apple.getWeight() > 150).or(apple -> Apple.Color.RED.equals(apple.getColor()));
filter(appleList, greenAndHeavyAppleOrRed);
# Reference
๋ชจ๋ ์๋ฐ ์ธ ์ก์ (ํ๋น๋ฏธ๋์ด)